package com.example.dj;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

//自定义的音乐列表适配
public class MusicListAdapter extends BaseAdapter{
	private Context context;		//上下文对象引�?
	private List<Mp3Info> mp3Infos;	//存放Mp3Info引用的集�?
	private Mp3Info mp3Info;		//Mp3Info对象引用
	private int pos = -1;			//列表位置
	

	//构�?函数
	public MusicListAdapter(Context context, List<Mp3Info> mp3Infos) {
		this.context = context;
		this.mp3Infos = mp3Infos;
	}

	@Override
	public int getCount() {
		return mp3Infos.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null)
		{
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.list, null);
			viewHolder.musicTitle = (TextView) convertView.findViewById(R.id.title);
			viewHolder.musicArtist = (TextView) convertView.findViewById(R.id.artist);
			viewHolder.musicDuration = (TextView) convertView.findViewById(R.id.duration);
			convertView.setTag(viewHolder);			//表示给View添加
		} else {
			viewHolder = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出
		}
		mp3Info = mp3Infos.get(position);
		viewHolder.musicTitle.setText(mp3Info.getTitle());			//显示标题
		viewHolder.musicArtist.setText(mp3Info.getArtist());		//显示歌手
		viewHolder.musicDuration.setText(MediaUtil.formatTime(mp3Info.getDuration()));//显示时长
		
		return convertView;
	}
	
	//定义�?��内部�?
	public class ViewHolder {
		//�?��控件对象引用
		public TextView musicTitle;		//音乐标题
		public TextView musicDuration;	//音乐时长
		public TextView musicArtist;	//音乐歌手
	}
}
